package com.android.sgzcommon.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.android.sgzcommon.take_photo.GetPhotoList;
import com.android.sgzcommon.take_photo.GetPhotoListImpl;
import com.android.sgzcommon.take_photo.ShowImages;
import com.android.sgzcommon.take_photo.ShowImagesImpl;
import com.android.sgzcommon.take_photo.entity.PhotoUpload;
import com.android.sgzcommon.take_photo.listener.OnPhotoClickListener;
import com.android.sgzcommon.take_photo.listener.OnPhotoUploadListener;
import com.android.sgzcommon.take_photo.listener.OnTakePhotoListListener;
import com.android.sgzcommon.take_photo.utils.Creator;

import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by sgz on 2019/12/27.
 */
public abstract class BaseGetPhotoListFragment<T extends PhotoUpload> extends BaseFragment implements GetPhotoList<T> {

    private RecyclerView mRvShowImages;
    private RecyclerView mRvTakePhotos;
    private GetPhotoListImpl mGetPhotoListImpl;
    private ShowImages mShowImages;

    protected abstract int getShowImageGridViewId();

    protected abstract int getTakePhotoGridViewId();

    protected abstract Creator<T> getCreator();

    protected abstract void onPhotos(List<T> ts, T t);

    @Override
    protected void init(Bundle savedInstanceState, View parent) {
        mRvShowImages = parent.findViewById(getShowImageGridViewId());
        mRvTakePhotos = parent.findViewById(getTakePhotoGridViewId());
        mShowImages = new ShowImagesImpl(mContext, mRvShowImages);
        Creator creator = getCreator();
        if (creator == null) {
            creator = new Creator<PhotoUpload>() {
                @Override
                public PhotoUpload create(String path) {
                    return new PhotoUpload(path);
                }
            };
        }
        mGetPhotoListImpl = new GetPhotoListImpl(getActivity(), creator, mRvTakePhotos);
        mGetPhotoListImpl.setOnPhotoListListener(new OnTakePhotoListListener<T>() {
            @Override
            public void onPhotos(List<T> ts, T t) {
                BaseGetPhotoListFragment.this.onPhotos(ts, t);
            }
        });
    }

    public void setImageUrls(List<String> urls) {
        mShowImages.setImageUrls(urls);
    }

    public void takePhoto() {
        mGetPhotoListImpl.takePhoto();
    }

    public void choosePhoto() {
        mGetPhotoListImpl.choosePhoto();
    }

    @Override
    public void setOnPhotoClickListener(OnPhotoClickListener listener) {
        mGetPhotoListImpl.setOnPhotoClickListener(listener);
    }

    @Override
    public void setOnPhotoListListener(OnTakePhotoListListener listener) {
        mGetPhotoListImpl.setOnPhotoListListener(listener);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("TakePhotoActivity", "onActivityResult: requestCode = " + requestCode);
        super.onActivityResult(requestCode, resultCode, data);
        mGetPhotoListImpl.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        mGetPhotoListImpl.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public void notifyShowPhotoChanged() {
        mShowImages.notifyPhotoChanged();
    }

    @Override
    public void notifyPhotoChanged() {
        mGetPhotoListImpl.notifyPhotoChanged();
    }

    @Override
    public void notifyPhotoChanged(int position) {
        mGetPhotoListImpl.notifyPhotoChanged(position);
    }

    @Override
    public List<T> getPhotoUploadList() {
        return mGetPhotoListImpl.getPhotoUploadList();
    }

    @Override
    public void addPhotoUpload(T t) {
        mGetPhotoListImpl.addPhotoUpload(t);
    }

    @Override
    public void addPhotoUploads(List<T> ts) {
        mGetPhotoListImpl.addPhotoUploads(ts);
    }

    @Override
    public void clearPhotos() {
        mGetPhotoListImpl.clearPhotos();
    }

    @Override
    public void uploadPhotos(String url, Map<String, String> data, Map<String, String> headers, OnPhotoUploadListener listener) {
        mGetPhotoListImpl.uploadPhotos(url, data, headers, listener);
    }
}
